package paziente;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Visualizza il profile di un Utente
 * 
 * @author sestari
 */
@WebServlet(name = "profiloUtente", urlPatterns = {"/profiloUtente"})
public class profiloUtente extends HttpServlet {

    /**
     * Crea la pagina HTML
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            try {

                ServletContext sc = getServletContext();
                Connection con = (Connection) sc.getAttribute("con");



                String id = request.getParameter("id");
                if (id == null) {
                    response.sendRedirect("welcome?msge=Erro! servlet chiamata senza parametri...");
                }

                String comand = "SELECT nome, cognome, data_nascita, foto FROM Persone where id =\"" + id + "\"";


                Statement st = (Statement) con.createStatement();
                ResultSet rs = (ResultSet) st.executeQuery(comand);


                rs.next();

                out.println("<!DOCTYPE html>  ");
                out.println("<html> ");
                out.println("    <head> ");
                out.println("        <title>Profilo Utente</title> ");
                out.println("    </head> ");
                out.println("    <body> ");

                out.println("<table border=\"1\">");


                out.println("<tr>");
                out.println("<td bgcolor=\"#DEDFF7\">");
                out.println("Nome:");
                out.println("</td>");

                out.println("<td bgcolor=\"white\">");
                out.println(rs.getString(1));
                out.println("</td>");

                out.println("</tr>");


                out.println("<tr>");
                out.println("<td bgcolor=\"#DEDFF7\">");
                out.println("Cognome:");
                out.println("</td>");

                out.println("<td bgcolor=\"white\">");
                out.println(rs.getString(2));
                out.println("</td>");

                out.println("</tr>");


                out.println("<tr>");
                out.println("<td bgcolor=\"#DEDFF7\">");
                out.println("Data nascita:");
                out.println("</td>");

                out.println("<td bgcolor=\"white\">");

                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

                out.println(dateFormat.format(rs.getDate(3)));
                out.println("</td>");


                out.println("</tr>");

                out.println("<tr>");
                out.println("<td bgcolor=\"#DEDFF7\">");
                out.println("Foto:");
                out.println("</td>");

                String realPath = getServletContext().getContextPath();
                out.println("<td bgcolor=\"white\">");
                out.println("<IMG SRC=\"" + realPath + rs.getString(4) + "\"  WIDTH=\"60\" HEIGHT=\"60\" ALT=\" " + rs.getString(4) + "\" \\>");
                out.println("</td>");




                out.println("</tr>");



                out.println("                <tr>                 ");
                out.println("                <form action=\"logoff\" method=\"post\"> ");
                out.println("                    <td  bgcolor=\"#DEDFF7\">");
                out.println("                        <input type=\"submit\"  value=\"Logoff\"/>                         ");
                out.println("                    </td>                   ");
                out.println("                </form> ");
                out.println("                    <td  bgcolor=\"#DEDFF7\"> ");
                out.println("                <form action=\"welcome\" method=\"post\"> ");
                out.println("                      <INPUT TYPE=\"button\" VALUE=\"Tornare\" onClick=\"history.go(-1);\">                         ");

                out.println("                </form> ");

                out.println("                </tr> ");




                out.println("</table>");

                out.println("    </body> ");
                out.println("</html> ");




            } finally {
                out.close();
            }
        } catch (Exception e) {
        }
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
